/*
 *
 */

package cn.jobstart.web.common;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtil {
    public static final String MOBILE_REGEX = "^1[3-9]\\d{9}$";
    //private static final String EMAIL_REGEX = "^([a-z0-9]+[-|\\.]?)+[a-z0-9]@([a-z0-9]+(-[a-z0-9]+)?\\.)+[a-z]{2,}$";
    private static final String EMAIL_REGEX = "^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$";

    public static boolean validate(String valContent, String regex) {
        Pattern pattern = Pattern.compile(regex, 0);
        Matcher matcher = pattern.matcher(valContent);
        return matcher.matches();
    }

    public static boolean validateType(String content, String regex) {
        Pattern pattern = Pattern.compile(regex, 42);
        Matcher matcher = pattern.matcher(content);
        return matcher.find();
    }

    public static String match(String content, String regex, int groupId) {
        Pattern pattern = Pattern.compile(regex, 42);

        if (groupId == 0) {
            return "";
        }

        Matcher matcher = pattern.matcher(content);
        StringBuffer buffer = new StringBuffer();
        while (matcher.find()) {
            buffer.append(matcher.group(groupId));
        }
        return buffer.toString();
    }

    public static Map<String, String> matchError(String content, String regex, int code, int msg) {
        Pattern pattern = Pattern.compile(regex, 42);

        if ((code == 0) || (msg == 0)) {
            return null;
        }

        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            Map<String, String> errors = new HashMap<String, String>();
            errors.put("code", matcher.group(code));
            errors.put("msg", matcher.group(msg));
            return errors;
        }
        return null;
    }

    public static String matchToken(String content, String regex, int groupId) {
        Pattern pattern = Pattern.compile(regex, 42);

        if (groupId == 0) {
            return "";
        }

        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            return matcher.group(groupId);
        }
        return "";
    }

    public static String matchUrls(String content, String regex, int groupId) {
        Pattern pattern = Pattern.compile(regex, 34);

        Matcher matcher = pattern.matcher(content);
        StringBuffer buffer = new StringBuffer();
        while (matcher.find()) {
            buffer.append(matcher.group(groupId)).append("$");
        }
        return buffer.toString();
    }

    public static List<String> matchList(String content, String regex, int groupId) {
        Pattern pattern = Pattern.compile(regex, 2);

        List<String> list = new ArrayList<String>();
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            list.add(matcher.group(groupId));
        }
        return list;
    }

    public static boolean valEmail(String email) {
        return validate(email, EMAIL_REGEX);
    }

    public static boolean valPhone(String phone) {
        return validate(phone, MOBILE_REGEX);
    }

    public static String clearHtml(String html) {
        String regEx = "<.+?>";

        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(html);
        String s = m.replaceAll("");

        return s;
    }

    public static String getResult(String xml) {
        return RegexUtil.match(xml, "\\<is_success\\>(.*?)\\</is_success\\>", 1);
    }

    public static Map<String, String> matchMore(String content, String regex, Map<String, Integer> params) {
        Pattern pattern = Pattern.compile(regex, 42);
        Matcher matcher = null;

        String key = "";
        int groupIndex = 0;
        Map<String, String> results = new HashMap<String, String>();
        Iterator<String> iter = params.keySet().iterator();

        while (iter.hasNext()) {
            key = iter.next();
            groupIndex = params.get(key);

            matcher = pattern.matcher(content);
            if (groupIndex == 0) {
                results.put(key, null);
            }
            if (matcher.find()) {
                results.put(key, matcher.group(groupIndex));
            }
        }

        return results;
    }

}
